package com.hangsu.train.components.service.impl;

import com.hangsu.train.components.constants.SecurityConstant;
import com.hangsu.train.components.convert.RoleConvert;
import com.hangsu.train.components.dao.RoleDao;
import com.hangsu.train.components.dao.RolePermissDao;
import com.hangsu.train.components.dao.UserRoleDao;
import com.hangsu.train.components.entity.Role;
import com.hangsu.train.components.entity.RolePermiss;
import com.hangsu.train.components.params.RolePermissParam;
import com.hangsu.train.components.params.RoleSearchParam;
import com.hangsu.train.components.service.RoleService;
import com.hangsu.train.core.keygen.KeyGenerate;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    private RoleDao roleDao;
    private UserRoleDao userRoleDao;
    private RolePermissDao rolePermissDao;
    private RoleConvert roleConvert;
    private KeyGenerate keyGenerate;

    @Resource
    public void setRoleDao(RoleDao roleDao) {
        this.roleDao = roleDao;
    }

    @Resource
    public void setRoleConvert(RoleConvert roleConvert) {
        this.roleConvert = roleConvert;
    }

    @Resource
    public void setRolePermissDao(RolePermissDao rolePermissDao) {
        this.rolePermissDao = rolePermissDao;
    }

    @Resource
    public void setUserRoleDao(UserRoleDao userRoleDao) {
        this.userRoleDao = userRoleDao;
    }

    @Resource
    public void setKeyGenerate(KeyGenerate keyGenerate) {
        this.keyGenerate = keyGenerate;
    }

    @Override
    public Page<Role> list(RoleSearchParam reqDto) {
        PageRequest request = PageRequest.of(reqDto.getPage(), reqDto.getPageSize());
        Role role = roleConvert.convert(reqDto);
        List<Role> list = roleDao.getRoleList(role, request.getOffset(), request.getPageSize());
        long count = roleDao.getRoleCount(role);
        return new PageImpl<>(list, request, count);
    }

    @Transactional
    @Override
    public boolean delRole(String code) {
        Role role = roleDao.getRoleByCode(code);
        if (role != null) {
            userRoleDao.delUserRole(role.getCode());
            rolePermissDao.delByRoleId(role.getId());
            roleDao.delRole(role.getId());
            return true;
        }
        return false;
    }

    @Override
    public boolean addRole(Role role) {
        role.setId((Long) keyGenerate.generateKey());
        role.setIsEdit(SecurityConstant.ROLE_EDIT_ENABLE);
        int ret = roleDao.insert(role);
        return ret > 0;
    }

    @Transactional
    @Override
    public boolean authz(RolePermissParam reqDto) {
        Role role = roleDao.getRoleByCode(reqDto.getRoleCode());
        List<RolePermiss> rolePermissions = new ArrayList<>();
        for (Long permissionId : reqDto.getPermissIds()) {
            RolePermiss rolePermiss = new RolePermiss();
            rolePermiss.setId((Long) keyGenerate.generateKey());
            rolePermiss.setRoleId(role.getId());
            rolePermiss.setPermissId(permissionId);
            rolePermissions.add(rolePermiss);
        }
        if (role != null) {
            rolePermissDao.delByRoleId(role.getId());
            rolePermissDao.insertBatch(rolePermissions);
            return true;
        }
        return false;
    }
}